package com.ld.security.common.mybatis.base.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.security.common.core.base.column.ListString;
import com.ld.security.common.core.base.ETreeEntity;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.mybatis.base.entity.ESortEntity;
import com.ld.security.common.mybatis.base.entity.TreeEntity;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.common.mybatis.util.TreeUtil;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基本树业务实现
 *
 * @param <E>
 */

public abstract class ITreeServiceImpl<E extends ETreeEntity<E>> extends ISuperServiceImpl<E> implements ITreeService<E> {



    public List<E> findByParentPathRightLikes(List<ListString> parentpaths) {

        EntityWrapper ew = this.getWrapper();
        for (ListString parentpath : parentpaths) {
            ew.or();
            ew.likeRight("parent_path",parentpath.getValue());


        }

        return superMapping.selectList(ew);
    }

    @Override
    public PageVo<E> getTree() {
        E root = this.newRoot();
        ListString listString =   new ListString(TreeEntity.root);
        List<E> list =this.findByParentPathRightLikes(Arrays.asList(listString));
        List<E> roots =  TreeUtil.dfsBuildTree(list);
        root.setChildren(roots);
        Page page = new Page<>();
        page.setRecords(Arrays.asList(root));
        page.setTotal(1);
        return  PageVo.of(page);
    }

    @Override
    public List<E> buildPageTree(List<E> page) {
        List<ListString> lists =  page.stream().map(val -> {
            ListString path =  new ListString();
            path.addAll(val.getParentPath());
            path.add(val.getId());
            return path;
        }).collect(Collectors.toList());

        List<E> list =  new LinkedList<>();
        list.addAll(page);

        if(!lists.isEmpty()){
            List<E>  children =  this.findByParentPathRightLikes(lists);
            list.addAll(children);
        }
        List<E> roots =  TreeUtil.dfsBuildTree(list);
        return  roots;
    }
    @Override
    public PageVo<E> pageTree(PagePo<E> po) {

        PageVo<E> page = this.page(po);
        List list =  buildPageTree(page.getList());
        page.setList(list);
        return page;
    }

    protected  E newRoot(){
        E root = this.newInstance();
        root.setId(TreeEntity.root);
        root.setLabel("根节点");
        return root;
    }



    @Override
    public void initTree(List<E> eList) {
        List<E> list = TreeUtil.treeToLink(eList);
        this.saveBatch(list);
    }

   static ListString root = new ListString();
    static {

        root.add(TreeEntity.root);
    }

    @Override
    public int saveTree(List<E> tree) {
     int i = 0;
        for (E e : tree) {
            if(ReflectionUtil.isNull(e.getParentPath())){
                e.setParentPath(root);
            }
            if(e instanceof ESortEntity){
                ESortEntity es = (ESortEntity)e;
                es.setSortVal(i++);
            }
            ReflectionUtil.fill(e);
        }
        int total =this.saveBatch(tree);
        for (E e : tree) {
            List<E> children = e.getChildren();
          if(ReflectionUtil.isNotNull(children)){
            //孩子参数初始化
              for (E child : children) {
                ListString parentPath = new ListString();
                parentPath.addAll(e.getParentPath());
                parentPath.add(e.getId());
                child.setParentPath(parentPath);
            }
           total+= this.saveTree(children);
          }
        }
        return total;
    }
}
